package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Intervention;
import com.itheima.service.InterventionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/intervention")
public class InterventionController {
    @Reference
    private InterventionService interventionService;

    @RequestMapping("/add")
    public Result add(@RequestBody Intervention intervention, Integer[] sportsIds, Integer[] foodIds){
        try {
            interventionService.add(intervention,sportsIds,foodIds);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Intervention intervention=interventionService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,intervention);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findByOrderId")
    public Result findByOrderId(Integer id){
        try {
            Intervention intervention=interventionService.findByOrderId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,intervention);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/findsportsIdByOrderId")
    public Result findsportsIdByOrderId(Integer id){
        try {
            List<Integer> list =interventionService.findsportsIdByOrderId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/findfoodIdsByOrderId")
    public Result findfoodIdsByOrderId(Integer id){
        try {
            List<Integer> list =interventionService.findfoodIdsByOrderId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Intervention intervention, Integer[] sportsIds, Integer[] foodIds){
        try {
            interventionService.edit(intervention,sportsIds,foodIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

}
